#![allow(unused)]

use {
    app, std::{io, time::Duration}, tokio::time, tracing::{info, Level}, tracing_appender::{
        non_blocking::WorkerGuard,
        rolling::{RollingFileAppender, Rotation},
    }, tracing_subscriber::{
        fmt::{writer::MakeWriterExt, Layer},
        prelude::*,
    }, tron, wallet
};

fn log_dir() -> &'static str {
    if cfg!(target_os = "linux") {
        "/root/logs"
    } else if cfg!(target_os = "windows") {
        "D:/logs"
    } else if cfg!(target_os = "macos") {
        "/Users/raymond/sol_works/logs"
    } else {
        "./logs"
    }
}

fn debug_log_file() -> &'static str {
    "dapp_debug.log"
}
fn info_log_file() -> &'static str {
    "dapp_info.log"
}
fn err_log_file() -> &'static str {
    "dapp_err.log"
}

fn setup_logs() -> Result<
    (
        Option<WorkerGuard>,
        Option<WorkerGuard>,
        Option<WorkerGuard>,
    ),
    anyhow::Error,
> {
    let stdout_layer = Layer::new()
        .with_ansi(true)
        .with_level(true)
        .with_thread_ids(true)
        .with_file(true)
        .with_line_number(true)
        .with_writer(
            io::stderr
                .with_max_level(Level::WARN)
                .or_else(io::stdout.with_max_level(Level::INFO)),
        );

    let debug_file = RollingFileAppender::builder()
        .rotation(Rotation::DAILY)
        .filename_prefix(debug_log_file())
        .max_log_files(10)
        .build(log_dir())
        .expect("initializing debug rolling file appender failed");
    let (debug, debug_guard) = tracing_appender::non_blocking(debug_file);
    let debug_layer = Layer::new()
        .with_ansi(false)
        .compact()
        .with_level(true)
        .with_file(true)
        .with_line_number(true)
        .with_thread_ids(true)
        .with_writer(
            debug
                .with_max_level(Level::DEBUG)
                .with_min_level(Level::DEBUG),
        );

    let info_file = RollingFileAppender::builder()
        .rotation(Rotation::DAILY)
        .filename_prefix(info_log_file())
        .max_log_files(10)
        .build(log_dir())
        .expect("initializing info rolling file appender failed");
    let (info, info_guard) = tracing_appender::non_blocking(info_file);
    let info_layer = Layer::new()
        .with_ansi(false)
        .compact()
        .with_level(true)
        .with_file(true)
        .with_line_number(true)
        .with_thread_ids(true)
        .with_writer(info.with_max_level(Level::INFO).with_min_level(Level::INFO));

    let err_file = RollingFileAppender::builder()
        .rotation(Rotation::DAILY)
        .filename_prefix(err_log_file())
        .max_log_files(10)
        .build(log_dir())
        .expect("initializing error rolling file appender failed");
    let (err, err_guard) = tracing_appender::non_blocking(err_file);
    let err_layer = Layer::new()
        .with_ansi(false)
        .compact()
        .with_level(true)
        .with_file(true)
        .with_line_number(true)
        .with_thread_ids(true)
        .with_writer(err.with_max_level(Level::WARN));

    let sub = tracing_subscriber::registry()
        .with(stdout_layer)
        .with(debug_layer)
        .with(info_layer)
        .with(err_layer);

    tracing::subscriber::set_global_default(sub)?;

    Ok((Some(debug_guard), Some(info_guard), Some(err_guard)))
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    #[allow(unused_variables)]
    let (o1, o2, o3) = setup_logs()?;

    app::bootstrap(commons::common::ENV).await?;
    //let w = wallet::generate_wallet();
    // tron::test_token(commons::common::ENV).await?;
    
    info!("startup finished!");
    Ok(())
}


fn parse_hex_to_decimal(hex_str: &str) -> Result<u128, std::num::ParseIntError> {
    u128::from_str_radix(hex_str, 16)
}